(function () {
	etlinking.views.groupMember = new GreateMemberFunc();

	function GreateMemberFunc() {
		var _self = this;

		_self.init = init;

	
		var _groupId = parseInt($("#txtGroupId").val()),
			_userGroupRole = parseInt($("#txtUserGroupRole").val()),
			$btnInvite = $("#btnInvite"),
			_friendGrid = null,
			_groupMemberGrid = null;
		var socialActions = nsbase.config.actionUrl.social,
			regions = nsbase.config.region,
        	utilities = nsbase.utility,
        	clientResource = nsbase.resources.clientResource;

        var $txtSearch = $("#txtSearch"),
			$btnSearch = $("#btnSearch");
		function init(){
			//initGroupPro();
			bindSearchContianer();
			bindFriedGrid();
			bindGroupMemberGrid();
		}
		
		function bindSearchContianer() {
			$("#searchContainer").show();
			$btnInvite.hide();
			$btnSearch.click(function() {
				var searchValue = getSearchValue();
				if(!searchValue){
					return;
				}

				bindFriedGrid();
			});
		}

		function getSearchValue () {
			var searchValue = $txtSearch.val();
			if(searchValue && searchValue == $txtSearch.attr("default-value")){
				searchValue = "";
			}
			return searchValue;
		}
		function bindFriedGrid () {
			var gridConfig = {
				renderTo:"friendContainer",
				height:500,
				rowNumber:true,
				columns:[
					//{title:"",dataIndex:"rankchange",width:30,formater:rankingFormatter},
					{title:"",dataIndex:"photo",width:40,formater:photoFormatter},
					{title:clientResource.columnFriendName,dataIndex:"user_name",width:200,align:"left"},
					{title:clientResource.columnFriendCalorie,dataIndex:"calorie",width:145,align:"left",sortable:true,selectable:true,formater:colorieFormatter},
					//{title:clientResource.columnFriendMiles,dataIndex:"miles",width:150,sortable:true,selectable:true},
					//{title:clientResource.columnFriendSteps,dataIndex:"steps",width:150,sortable:true,selectable:true},
					//{title:clientResource.columnFriendSleep,dataIndex:"sleeptime",width:150,sortable:true,selectable:true},
					{title:clientResource.columnInvite,dataIndex:"",width:100,formater:inviteFormater}
				],
				rowSelectable:"none",
				paggingBar: {
					remote: true,
					pageIndex: 1,
					pageSize: 8
				},
				store:{
					data: {
						searchName: getSearchValue(),
						sortField:"user_name",
						sortDirect:"ASC"
					}, 
					url: socialActions.searchFriends,
				}
			}
			var gridEvents = nsbase.controls.grid.setting.eventEnums;
			_friendGrid = new nsbase.controls.grid(gridConfig);
			_friendGrid.on(gridEvents.rowClick,function(sender,rowData){
				
			});
			_friendGrid.on(gridEvents.columnSelectChanged,function(columnEl,rowData){
				
			});
			_friendGrid.on(gridEvents.cellClick,function(event,rowData){
				var sender = $(event.target);
				if(sender.is(".add")){
					event.stopPropagation();
					inviteFriend(rowData.user_id);
				}
			});

			// var dataList = [
			// 	{'ranking': 0, 'user_id': 837, 'sleeptime': 0, 'type': 1, 'jumps': 0, 'miles': 0, 'rankchange': 0, 'photo': '', 'relation': 1, 'calorie': 0, 'user_name': 'test123'},
			// 	{'ranking': 0, 'user_id': 837, 'sleeptime': 0, 'type': 1, 'jumps': 0, 'miles': 0, 'rankchange': 1, 'photo': '', 'relation': 1, 'calorie': 0, 'user_name': 'test123'},
			// 	{'ranking': 0, 'user_id': 837, 'sleeptime': 0, 'type': 1, 'jumps': 0, 'miles': 0, 'rankchange': -1, 'photo': '', 'relation': 2, 'calorie': 0, 'user_name': 'test123'},
			// 	{'ranking': 0, 'user_id': 837, 'sleeptime': 0, 'type': 1, 'jumps': 0, 'miles': 0, 'rankchange': 0, 'photo': '', 'relation': 2, 'calorie': 0, 'user_name': 'test123'},
			// 	{'ranking': 0, 'user_id': 837, 'sleeptime': 0, 'type': 1, 'jumps': 0, 'miles': 0, 'rankchange': 0, 'photo': '', 'relation': 2, 'calorie': 0, 'user_name': 'test123'},
			// 	{'ranking': 0, 'user_id': 837, 'sleeptime': 0, 'type': 1, 'jumps': 0, 'miles': 0, 'rankchange': 1, 'photo': '', 'relation': 2, 'calorie': 0, 'user_name': 'test123'},
			// 	{'ranking': 0, 'user_id': 837, 'sleeptime': 0, 'type': 1, 'jumps': 0, 'miles': 0, 'rankchange': -1, 'photo': '', 'relation': 2, 'calorie': 0, 'user_name': 'test123'},

			// ];
			// grid.showData(dataList);

			function rankingFormatter(column, value, obj){
				var rankingEnum = nsbase.enums.rankingEnum.getEnumByValue(value);
				var cssName = rankingEnum.key;
				return '<span class="grid_icon '+ cssName +'"></span>';
			}

			function photoFormatter (column,value,obj) {
				var imgSrc = "../static/images/photo.png";
				if(value){
					imgSrc = value;
				}
				return '<img src="'+ imgSrc +'" width="30" height="30" onError="this.src=\'../static/images/photo.png\';"/>';
			}
			
			function colorieFormatter (column,value,obj) {
				return '<span class="grid_icon calorie"></span>' + value + ' cal';
			}
			function inviteFormater (column,value,obj) {
				return '<a href="###"><span class="grid_icon add"></span></a>';
			}
		}

		function bindGroupMemberGrid () {
			var gridConfig = {
				renderTo:"memberContainer",
				height:500,
				rowNumber:true,
				columns:[
					//{title:"",dataIndex:"rankchange",width:30,formater:rankingFormatter},
					{title:"",dataIndex:"photo",width:40,formater:photoFormatter},
					{title:clientResource.columnGroupMemberName,dataIndex:"user_name",width:240,align:"left",formater:nameFormatter},
					//{title:clientResource.columnGroupCalorie,dataIndex:"calorie",width:150,align:"left",sortable:true,selectable:true,formater:colorieFormatter},
					//{title:clientResource.columnGroupMiles,dataIndex:"miles",width:150,sortable:true,selectable:true},
					//{title:clientResource.columnGroupSteps,dataIndex:"steps",width:150,sortable:true,selectable:true},
					//{title:clientResource.columnGroupSleep,dataIndex:"sleeptime",width:150,sortable:true,selectable:true},
					{title:clientResource.columnGroupMemberManage,dataIndex:"relation",width:100,formater:removeFormater}
				],
				rowSelectable:"none",
				paggingBar: {
					remote: true,
					pageIndex: 1,
					pageSize: 8
				},
				store:{
					data: {
						groupId: _groupId
					}, 
					url: socialActions.getGroupMembers,
				}
			}
			var gridEvents = nsbase.controls.grid.setting.eventEnums;
			_groupMemberGrid = new nsbase.controls.grid(gridConfig);
			_groupMemberGrid.on(gridEvents.rowClick,function(sender,rowData){
				
			});
			_groupMemberGrid.on(gridEvents.columnSelectChanged,function(columnEl,rowData){
				
			});
			_groupMemberGrid.on(gridEvents.cellClick,function(event,rowData){
				var sender = $(event.target);
				if(sender.is(".del")){
					event.stopPropagation();
					removeMember(rowData.user_id);
				}
				if(sender.is(".set_admin")){
					event.stopPropagation();
					setGroupAdmin(rowData.user_id);
				}
				if(sender.is(".remove_admin")){
					event.stopPropagation();
					removeGroupAdmin(rowData.user_id);
				}
			});

			// var dataList = [
			// 	{'ranking': 0, 'user_id': 837, 'sleeptime': 0, 'type': 1, 'jumps': 0, 'miles': 0, 'rankchange': 0, 'photo': '', 'relation': 1, 'calorie': 0, 'user_name': 'test123'},
			// 	{'ranking': 0, 'user_id': 837, 'sleeptime': 0, 'type': 1, 'jumps': 0, 'miles': 0, 'rankchange': 1, 'photo': '', 'relation': 1, 'calorie': 0, 'user_name': 'test123'},
			// 	{'ranking': 0, 'user_id': 837, 'sleeptime': 0, 'type': 1, 'jumps': 0, 'miles': 0, 'rankchange': -1, 'photo': '', 'relation': 2, 'calorie': 0, 'user_name': 'test123'},
			// 	{'ranking': 0, 'user_id': 837, 'sleeptime': 0, 'type': 1, 'jumps': 0, 'miles': 0, 'rankchange': 0, 'photo': '', 'relation': 2, 'calorie': 0, 'user_name': 'test123'},
			// 	{'ranking': 0, 'user_id': 837, 'sleeptime': 0, 'type': 1, 'jumps': 0, 'miles': 0, 'rankchange': 0, 'photo': '', 'relation': 2, 'calorie': 0, 'user_name': 'test123'},
			// 	{'ranking': 0, 'user_id': 837, 'sleeptime': 0, 'type': 1, 'jumps': 0, 'miles': 0, 'rankchange': 1, 'photo': '', 'relation': 2, 'calorie': 0, 'user_name': 'test123'},
			// 	{'ranking': 0, 'user_id': 837, 'sleeptime': 0, 'type': 1, 'jumps': 0, 'miles': 0, 'rankchange': -1, 'photo': '', 'relation': 2, 'calorie': 0, 'user_name': 'test123'},

			// ];
			// grid.showData(dataList);

			function rankingFormatter(column, value, obj){
				var rankingEnum = nsbase.enums.rankingEnum.getEnumByValue(value);
				var cssName = rankingEnum.key;
				return '<span class="grid_icon '+ cssName +'"></span>';
			}

			function photoFormatter (column,value,obj) {
				var imgSrc = "../static/images/photo.png";
				if(value){
					imgSrc = value;
				}
				return '<img src="'+ imgSrc +'" width="30" height="30" onError="this.src=\'../static/images/photo.png\';"/>';
			}
			function nameFormatter (column,value,obj) {
				var memberRoleEnum = nsbase.enums.memberRoleEnum.getEnumByValue(obj.role);
				var cssName = memberRoleEnum.key;
				return '<span class="grid_icon" style="width:30px"></span><span class="grid_icon '+ cssName +'" title="'+ memberRoleEnum.text +'"></span>'+utilities.htmlEncode(value);
			}
			function colorieFormatter (column,value,obj) {
				return '<span class="grid_icon calorie"></span>' + value + ' cal';
			}
			function removeFormater (column,value,obj) {
				var cellHtml = '';
				var memberRoleEnum = nsbase.enums.memberRoleEnum;
				var memberRole = memberRoleEnum.getEnumByValue(obj.role); 
				if(_userGroupRole == memberRoleEnum.creator){
					if(memberRole == memberRoleEnum.admin){
						cellHtml += '<a href="###" title="'+ clientResource.linkRemoveManager +'"><span class="grid_icon remove_admin"></span></a>'
					}
					if(memberRole == memberRoleEnum.member){
						cellHtml += '<a href="###" title="'+ clientResource.linkSetManager +'"><span class="grid_icon set_admin"></span></a>'
					}
					cellHtml += '&nbsp;&nbsp;<a href="###" title="'+ clientResource.linkRemoveMember +'"><span class="grid_icon del"></span></a>';
				}
				if(_userGroupRole == memberRoleEnum.admin && memberRole == memberRoleEnum.member){
					cellHtml = '<a href="###" title="'+ clientResource.linkRemoveMember +'"><span class="grid_icon del"></span></a>';
				}
				if(memberRole == memberRoleEnum.creator){
					cellHtml = '';
				}
				return cellHtml;
			}
		}

		function inviteFriend (friendId) {
			var param = {
					groupId:_groupId,
	            	friendId: friendId
				}
				nsbase.ajax.request(socialActions.inviteFriend,param,function(result){
	            	if(result.success){
	                	if(_groupMemberGrid){
	                		_groupMemberGrid.refresh();
	                	}
	            	} else {
	            		utilities.errorDialog(result.message);
	            	}
				},function () {
					utilities.errorDialog(clientResource.msgRequestFail);
				});
		}

		function removeMember (memberId) {
			var param = {
					groupId: _groupId,
	            	memberId: memberId
				}
				nsbase.ajax.request(socialActions.removeMember,param,function(result){
	            	if(result.success){
	                	_groupMemberGrid.refresh();
	            	} else {
	            		utilities.errorDialog(result.message);
	            	}
				},function () {
					utilities.errorDialog(clientResource.msgRequestFail);
				});
		}
		function setGroupAdmin(memberId){
			var param = {
				groupId:_groupId,
				memberId:memberId
			};
			nsbase.ajax.request(socialActions.setGroupAdmin,param,function(result){
	            if(result.success){
	                if(_groupMemberGrid){
			            _groupMemberGrid.refresh();
			        }
	            } else{
	            	utilities.errorDialog(result.message);
	            }
	        },function () {
	        	utilities.errorDialog(clientResource.msgRequestFail);
	        });
		}

		function removeGroupAdmin (memberId) {
			var param = {
				groupId:_groupId,
				memberId:memberId
			};
			nsbase.ajax.request(socialActions.removeGroupAdmin,param,function(result){
	            if(result.success){
	                if(_groupMemberGrid){
			            _groupMemberGrid.refresh();
			        }
	            } else{
	            	utilities.errorDialog(data.message);
	            }
	        },function () {
	        	utilities.errorDialog(clientResource.msgRequestFail);
	        });
		}
	}
})();